package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.User;
import com.bdqn.tour.service.impl.HzyInfoServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/updateInfo")
public class UpdateInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        // 优先从请求参数获取手机号
        String phone = req.getParameter("phone");

        // 如果请求参数中没有，再从session获取
        if (phone == null || phone.isEmpty()) {
            phone = (String) req.getSession().getAttribute("phone");
        }

        // 验证手机号
        if (phone == null || phone.isEmpty() || phone.contains("*")) {
            System.out.println("无效的手机号: " + phone);
            out.print(JSON.toJSONString(false));
            return;
        }

        // 获取其他表单数据
        String nickname = req.getParameter("nickname");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String career = req.getParameter("career");
        String qq = req.getParameter("qq");


        User user = new User();
        user.setIpone(phone);
        user.setNickname(nickname);
        user.setEmail(email);
        user.setSex(sex);
        user.setCareer(career);
        user.setQq(qq);

        boolean flag = new HzyInfoServiceImpl().updateUserInfo(user);

        out.print(JSON.toJSONString(flag));
        out.close();


    }
}
